package cn.itsource.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.config.anno.MyLog;
import cn.itsource.domain.SysUser;
import cn.itsource.mapper.SysUserMapper;
import cn.itsource.query.SysUserQuery;
import cn.itsource.service.BlogSysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 实现业务层的接口
 */
@Service
public class BlogSysUserServiceimpl implements BlogSysUserService {

//    注入mapper层
    @Autowired
    private SysUserMapper mapper;
    @Override
    @MyLog(value = "用户列表")
    public SysUser findOne(Long id) {
        //使用tk的方法查询一个
        return mapper.selectByPrimaryKey(id);
    }
    @Override
    public List<SysUser> findAll() {
        //使用tk的方法查询全部
        return mapper.selectAll();
    }
    @Override
    @MyLog(value = "用户列表")
    public void save(SysUser sysUser) {
//        使用tk的方法添加
        mapper.insertSelective(sysUser);

    }
    @Override
    @MyLog(value = "用户列表",isImportant = true)
    public void delete(Long id) {
        //        使用tk的方法删除
        mapper.deleteByPrimaryKey(id);


    }
    @Override
    @MyLog(value = "用户列表")
    public void update(SysUser sysUser) {
        //        使用tk的方法修改
        mapper.updateByPrimaryKey(sysUser);

    }


    @Override
    public PageInfo<SysUser> pageList(SysUserQuery sysUserQuery) {
        PageHelper.startPage(sysUserQuery.getPage(),sysUserQuery.getRows());
        //高级查询，封装查询条件 参数：查询的domain的class类型
        Example example = new Example(SysUser.class);
        //判断keyword是有效值
        if (StringUtils.isNotBlank(sysUserQuery.getKeyword())){
            example.and().andLike("userName","%"+sysUserQuery.getKeyword()+"%");
            example.or().orLike("realName","%"+sysUserQuery.getKeyword()+"%");
        }

        List<SysUser> sysUsers = mapper.selectByExample(example);
        //封装成pageInfo
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);
        //返回数据
        return pageInfo;
    }
//实现登录方法
    @Override
    public SysUser login(SysUser user) {
        //登录业务
        //1.验证参数  //如果是无效值
        if (StrUtil.isBlank(user.getUserName())){
//            抛出异常
           throw new RuntimeException("请输入用户名");
        }
        if (StrUtil.isBlank(user.getPassword())){
//            抛出异常
           throw new RuntimeException("请输入密码");
        }
        //2.根据用户名查询对象，如果查不到（返回是null），抛异常
        Example example = new Example(SysUser.class);
        //等同于查询 select * from employee where user_name = “值”
        example.and().andEqualTo("userName",user.getUserName());
//        查找这个用户名的实例对象
        SysUser sysUser = mapper.selectOneByExample(example);
        if (sysUser==null){
            throw new RuntimeException("用户名不存在");
        }
//        将用户输入的密码进行加密匹配
        String s = MD5.create().digestHex(user.getPassword());
        if (!s.equals(sysUser.getPassword())){
            throw new RuntimeException("密码错误!请检查后重试");
        }
//        到这里验证成功了,就把头像和真名回传
//        这边创建一个新的对象，将信息封装后回传
        SysUser newSysUser = new SysUser();
        newSysUser.setRealName(sysUser.getRealName());
        newSysUser.setAvatar(sysUser.getAvatar());
        newSysUser.setId(sysUser.getId());
        return newSysUser;
    }


}
